<?php
/**
 * Created by PhpStorm.
 * User: J.H
 * Date: 2017/2/28
 * Time: 14:29
 */

namespace lib\oauth\weibo;


use lib\curl\HttpRequest;
use lib\oauth\OauthBase;

class WeiboConnect extends OauthBase
{

    private $url_api = 'https://api.weibo.com/';
    private $app_key;
    private $app_secret;
    private $app_callback;
    private $api_scope = 'all';
    protected $access_token;
    protected $uid;


    function __construct($config = array('appKey' => '', 'appSecret' => '', 'callback' => ''))
    {
        parent::__construct();
        $this->app_key = $config['appKey'];
        $this->app_secret = $config['appSecret'];
        $this->app_callback = $config['callback'];
    }

    /**
     * @param $scope
     *
     * @link http://open.weibo.com/wiki/Scope#scope.E6.9D.83.E9.99.90.E6.8E.A5.E5.8F.A3
     * @return $this
     */
    function setScope($scope)
    {
        $this->api_scope = $scope;
        return $this;
    }

    private function login()
    {
        $params = array(
            'client_id' => $this->app_key,
            'redirect_uri' => $this->app_callback,
            'scope' => $this->api_scope,
            'state' => $this->setState('wb'),
        );
        $url = $this->parseUrl($this->url_api, 'oauth2/authorize', $params);
        $this->to($url);
    }

    /**
     * 获取access_token
     * @return string
     */
    private function getAccessToken()
    {
        if ($this->access_token) {
            return $this->access_token;
        }

        if (empty($_GET['code']) && empty($_GET['state'])) {
            $this->login();
        }

        $state = $this->getState('wb');
        if (isset($_GET['state']) && $_GET['state'] !== $state) {
            $this->error(0);
        }
        if (!isset($_GET['code'])) {
            $this->error(1);
        }

        $params = array(
            'client_id' => $this->app_key,
            'client_secret' => $this->app_secret,
            'grant_type' => 'authorization_code',
            'code' => $_GET['code'],
            'redirect_uri' => $this->app_callback
        );
        $url = $this->parseUrl($this->url_api, 'oauth2/access_token');
        $http = new HttpRequest();
        $result = $http->setOptions(CURLOPT_POST, true)->request($url, $params);
        if ($result = json_decode($result, true)) {
            if (isset($result['access_token'])) {
                $this->uid = $result['uid'];
                return $this->access_token = $result['access_token'];
            }
        }
        return '';
    }

    /**
     * @see getUid()
     * @return mixed
     */
    function getOpenid()
    {
        return $this->getUid();
    }

    /**
     * @see $this->getAccessToken()
     * @return mixed
     */
    function getUid()
    {
        if ($this->uid) {
            return $this->uid;
        }
        $this->getAccessToken();
        return $this->uid;
    }

    /**
     * @return array
     * @link http://open.weibo.com/wiki/2/users/show#.E8.BF.94.E5.9B.9E.E7.BB.93.E6.9E.9C
     */
    function getUserInfo()
    {
        $params = array(
            'access_token' => $this->getAccessToken(),
            'uid' => $this->getUid()
        );
        $url = $this->parseUrl($this->url_api, '2/users/show.json', $params);
        $result = HttpRequest::get($url);
        if (!$user_info = json_decode($result, true)) {
            return array();
        }
        return $user_info;
    }
}